package com.pureut.system.controller;

import com.pureut.common.core.web.controller.BaseController;
import com.pureut.common.core.web.domain.AjaxResult;
import com.pureut.common.core.web.page.TableDataInfo;
import com.pureut.common.log.annotation.Log;
import com.pureut.common.log.enums.BusinessType;
import com.pureut.common.security.annotation.RequiresPermissions;
import com.pureut.system.domain.dto.MaterialWasteDto;
import com.pureut.system.domain.MaterialWaste;
import com.pureut.system.domain.MaterialCoding;
import com.pureut.system.domain.vo.MaterialWasteVo;
import com.pureut.system.export.MaterialWasteExport;
import com.pureut.system.service.IMaterialCodingService;
import com.pureut.system.service.IMaterialWasteService;
import com.pureut.system.util.ExcelUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.List;

/**
 * @Description 废料信息
 * @Param
 * @Date 22-11-24
 * @Author hl
 **/
@RestController
@RequestMapping("/waste")
public class MaterialWasteController extends BaseController {

    @Resource
    private IMaterialWasteService materialWasteService;

    @Resource
    private IMaterialCodingService materialCodingService;

    /**
     * 获取废料配置
     */
    @ApiOperation("查询废料信息")
    @RequiresPermissions("material:waste:list")
    @GetMapping("/list")
    public TableDataInfo list(MaterialWasteVo MaterialWaste)
    {
        startPage();
        List<MaterialWasteDto> list = materialWasteService.selectWasteList(MaterialWaste);
        return getDataTable(list);
    }

    /**
     * 新增类型
     */
    @RequiresPermissions("material:waste:addType")
    @Log(title = "废料类型", businessType = BusinessType.INSERT)
    @PostMapping("/addType")
    public AjaxResult add(@RequestBody MaterialCoding materialCoding) throws Exception {
        return toAjax(materialCodingService.codingAdd(materialCoding));
    }

    /**
     * 修改类别
     */
    @RequiresPermissions("material:waste:editType")
    @Log(title = "废料类别", businessType = BusinessType.UPDATE)
    @PutMapping("/editType")
    public AjaxResult editType(@RequestBody MaterialCoding materialCoding) throws Exception {
        return toAjax(materialCodingService.updateCodingEdit(materialCoding));
    }

    /**
     * 删除类别
     */
    @RequiresPermissions("material:waste:removeType")
    @Log(title = "废料类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/removeType")
    public AjaxResult remove(long codingId) throws Exception {
        return toAjax(materialCodingService.deleteCodingById(codingId));
    }

    /**
     * 新增废料信息
     */
    @RequiresPermissions("material:waste:add")
    @Log(title = "废料信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody MaterialWaste materialWaste) throws Exception {
        return toAjax(materialWasteService.insertWaste(materialWaste));
    }

    /**
     * 删除废料信息
     */
    @RequiresPermissions("material:waste:remove")
    @Log(title = "废料信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{materialId}")
    public AjaxResult remove(@PathVariable("materialId") String materialId) throws Exception {
        return toAjax(materialWasteService.deleteWasteById(materialId));
    }

    /**
     * 根据编号获取详细信息
     */
    //@RequiresPermissions("material:waste:query")
    @GetMapping(value = "/{materialId}")
    public AjaxResult getWasteInfo(@PathVariable long materialId)
    {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("detailed",materialWasteService.selectWasteById(materialId));
        return ajax;
    }

    /**
     * 修改废料信息
     */
    @RequiresPermissions("material:waste:edit")
    @Log(title = "废料信息", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody MaterialWaste materialWaste) throws Exception {
        return toAjax(materialWasteService.updateMateriaWasteEdit(materialWaste));
    }


    /**
     * 废料冻结
     */
    @RequiresPermissions("material:waste:editFreeze")
    @Log(title = "废料信息", businessType = BusinessType.UPDATE)
    @PutMapping("/editFreeze")
    public AjaxResult editFreeze(@RequestBody MaterialWaste materialWaste){
        return toAjax(materialWasteService.updateWasteEditFreeze(materialWaste));
    }

    /**
     * 废料解冻
     */
    @RequiresPermissions("material:waste:editThaw")
    @Log(title = "废料信息", businessType = BusinessType.UPDATE)
    @PutMapping("/editThaw")
    public AjaxResult editThaw(@RequestBody MaterialWaste materialWaste){
        return toAjax(materialWasteService.updateWasteEditFreeze(materialWaste));
    }

    /**
     * 树结构
     *
     * @param
     * @return 废料
     */
    @GetMapping("/listChildren")
    public AjaxResult listChildren()
    {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("theDrop", materialWasteService.selectChildrenWasteThe());
        return ajax;
    }

    /**
     * 生成废料导入模板
     */
    @PostMapping("/downTemplate")
    public void downTemplate(HttpServletResponse response) throws IOException {
        String fileName = "C:/Users/jack/Desktop/导入导出模板/导入模板/导入废料信息.xlsx";
        File file = new File(fileName);
        ByteArrayOutputStream baos = null;
        ServletOutputStream out = null;
        try {

            InputStream inStream=new FileInputStream(file);
            byte[] buffer = new byte[1024];
            int len;
            baos = new ByteArrayOutputStream();
            while ((len = inStream.read(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }
            response.setContentType("application/octet-stream;charset=UTF-8");
            response.setHeader("Content-Disposition","attachment;filename=" + URLEncoder.encode("导入废料信息.xlsx","UTF-8"));
            out = response.getOutputStream();
            out.write(baos.toByteArray());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            baos.flush();
            baos.close();
            out.flush();
            out.close();
        }
    }

    /**
     * 废料信息导入
     */
    @PostMapping("/materialWasteImport")
    @RequiresPermissions("material:waste:import")
    @Log(title = "废料信息导入", businessType = BusinessType.INSERT)
    public AjaxResult materialWasteImport(MultipartFile file) throws IOException {
        ExcelUtil<MaterialWasteExport> excelUtil = new ExcelUtil<MaterialWasteExport>(MaterialWasteExport.class);
        List<MaterialWasteExport> coCustomer = excelUtil.importExcel(file.getInputStream());
        return AjaxResult.success(materialWasteService.importMouldData(coCustomer));
    }

}